package ai.people.platform.oss.controller;

import ai.people.netmon.framework.domain.oss.vo.UploadVO;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.platform.oss.service.OssStorageService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * oss加密
 *
 * @author yuanqinglong
 * @date 2022/4/28 17:31
 */
@Validated
@RefreshScope
@RestController
@RequiredArgsConstructor
public class EncryptOssController {



    /**
     * 启用的存储服务：未配置默认阿里oss
     */
    @Value("#{T(ai.people.core.common.util.ApplicationContextUtil).getBean('${oss.storage.enable:aliOssStorage}')}")
    private OssStorageService ossStorageServiceImpl;


    /**
     * 上传图片
     *
     * @param multipartFile 文件
     * @return {@link Result}<{@link UploadVO}>
     * @throws IOException 异常
     */
    @PostMapping("/picture/upload")
    public Result<UploadVO> uploadPicture(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        return ResultUtil.success(ossStorageServiceImpl.uploadPicture(multipartFile));
    }


    /**
     * 上传文件
     *
     * @param multipartFile 多部分文件
     * @return {@link Result}<{@link UploadVO}>
     * @throws Exception 异常
     */
    @PostMapping("/file/upload")
    public Result<UploadVO> uploadFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        return ResultUtil.success(ossStorageServiceImpl.uploadFile(multipartFile));
    }

    /**
     * 获取图片
     *
     * @param pictureName         照片名字
     * @param httpServletRequest  http servlet请求
     * @param httpServletResponse http servlet响应
     * @throws Exception 异常
     */
    @GetMapping("/picture/obtain")
    public void obtainPicture(@RequestParam("fileName") String pictureName, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ossStorageServiceImpl.write(pictureName, httpServletRequest, httpServletResponse);
    }

    /**
     * 获取文件
     *
     * @param fileName         照片名字
     * @param httpServletRequest  http servlet请求
     * @param httpServletResponse http servlet响应
     * @throws Exception 异常
     */
    @GetMapping("/file/obtain")
    public void obtainFile(@RequestParam("fileName") String fileName, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ossStorageServiceImpl.writeFile(fileName, httpServletRequest, httpServletResponse);
    }
}
